package com.meng.webconfig;

import com.meng.Interceptor.LoginAndLogoutInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


/**
 * 配置静态访问资源 页面跳转 配置拦截器
 *
 * @author MENG
 * @version 2017/12/22
 * @see
 */
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter
{

    /**
     * 登录拦截器
     */
    @Autowired
    private LoginAndLogoutInterceptor loginAndLogoutInterceptor;

    /**
     *
     * 跳转首页
     *
     * 以前要访问一个页面需要先创建个Controller控制类，再写方法跳转到页面
     * 在这里配置后就不需要那么麻烦了，直接访问http://localhost:8080/toIndex就跳转到Index.html页面了
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry)
    {
        registry.addViewController("/err/404").setViewName("/common/404");

        registry.addViewController("/err/500").setViewName("/common/500");

        registry.addViewController("/err/401").setViewName("/common/401");

        registry.addViewController("/toIndex").setViewName("index");

        registry.addViewController("/toLogin").setViewName("common/to_login");

        super.addViewControllers(registry);
    }


    /**
     *
     * 配置拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
        /**
         * "toLogin" 认证中心登录页面
         */
        registry.addInterceptor(loginAndLogoutInterceptor).addPathPatterns("/**").excludePathPatterns("/toLogin","/toLogout","/err/*");

        super.addInterceptors(registry);
    }




    /**
     * 配置静态访问资源  带测试
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        //      以增加 /my/* 映射到 classpath:/my/*

//        registry.addResourceHandler("/my/**").addResourceLocations("classpath:/my/");

        //      也可以配置系统的路径
//        registry.addResourceHandler("/my/**").addResourceLocations("file:H:/my/");

        super.addResourceHandlers(registry);
    }


}

